﻿using System;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SAC.sqlHelper;
using SAC.Elink;
using SAC.Helper;
using System.Collections;
using System.Data;
using SAC.Power;
using SAC.Plink;
using SAC.DB2;

namespace DAL
{
    public class DALBZ
    {
        string rlDBType = "";
        string rtDBType = "";

        string pGl1 = "WHSIS.U1APSH.U1A04013";
        string pGl2 = "WHSIS.U2APSH.U2A04013";

        public DataTable GetAllBZ(out string errMsg)
        {
            this.init();
            errMsg = "";

            //string sql1 = "select min(ID_KEY) as ID_KEY,UGROUP from WebUser group by UGROUP order by ID_KEY";
            string sql = "select 班组编号,班组描述 from 班组表 order by 班组编号";

            DataTable dt;

            if (rlDBType == "SQL")
            {
                dt = DBsql.RunDataTable(sql, out errMsg);
            }
            else
            {
                dt = DBdb2.RunDataTable(sql, out errMsg);
            }
            return dt;
        }

        public bool InsertBZ(int BzId, string BzMs, out string errMsg)
        {
            errMsg = "";
            bool flag = false;

            this.init();

            string sql1 = "select * from 班组表 where 班组编号=" + BzId + "";

            DataTable dt;

            if (rlDBType == "SQL")
            {
                dt = DBsql.RunDataTable(sql1, out errMsg);
            }
            else
            {
                dt = DBdb2.RunDataTable(sql1, out errMsg);
            }
            if (dt == null || dt.Rows.Count == 0)
            {
                string sql2 = "insert into 班组表 (班组编号,班组描述) values (" + BzId + ",'" + BzMs + "')";
                if (rlDBType == "SQL")
                {
                    DBsql.RunNonQuery(sql2, out errMsg);
                }
                else
                {
                    DBdb2.RunNonQuery(sql2, out errMsg);
                }
                if (errMsg == "")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        public bool UpdateBZ(int OBzId, int BzId, string BzMs, out string errMsg)
        {
            errMsg = "";
            this.init();
            if (OBzId == BzId)
            {
                string sql = "update 班组表 set 班组描述='" + BzMs + "' where 班组编号=" + OBzId + "";
                if (rlDBType == "SQL")
                {
                    DBsql.RunNonQuery(sql, out errMsg);
                }
                else
                {
                    DBdb2.RunNonQuery(sql, out errMsg);
                }
                if (errMsg == "")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                string sql1 = "select * from 班组表 where 班组编号=" + BzId + "";

                DataTable dt;

                if (rlDBType == "SQL")
                {
                    dt = DBsql.RunDataTable(sql1, out errMsg);
                }
                else
                {
                    dt = DBdb2.RunDataTable(sql1, out errMsg);
                }
                if (dt==null || dt.Rows.Count == 0)
                {
                    string sql2 = "update 班组表 set 班组编号=" + BzId + ",班组描述='" + BzMs + "' where 班组编号=" + OBzId + "";
                    if (rlDBType == "SQL")
                    {
                        DBsql.RunNonQuery(sql2, out errMsg);
                    }
                    else
                    {
                        DBdb2.RunNonQuery(sql2, out errMsg);
                    }
                    if (errMsg == "")
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
        }

        public bool DeleteBZ(int BzId,out string errMsg)
        {
            errMsg = "";
            bool flag = true;
            this.init();
            
            string sql = "delete from 班组表 where 班组编号=" + BzId + "";
            if (rlDBType == "SQL")
            {
                DBsql.RunNonQuery(sql, out errMsg);
            }
            else
            {
                DBdb2.RunNonQuery(sql, out errMsg);
            }
            if (errMsg == "")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public DataTable GetAllBC(out string errMsg)
        {
            this.init();
            errMsg = "";

            //string sql1 = "select min(ID_KEY) as ID_KEY,UGROUP from WebUser group by UGROUP order by ID_KEY";
            string sql = "select 班次编号,班次描述,起始时间,结束时间 from 班次时间表 order by 班次编号";

            DataTable dt;

            if (rlDBType == "SQL")
            {
                dt = DBsql.RunDataTable(sql, out errMsg);
            }
            else
            {
                dt = DBdb2.RunDataTable(sql, out errMsg);
            }
            return dt;
        }

        public bool InsertBC(string BcId, string BcMs, string ST, string ET, out string errMsg)
        {
            errMsg = "";
            bool flag = false;

            this.init();

            string sql1 = "select * from 班次时间表 where 班次编号='" + BcId + "'";

            DataTable dt;

            if (rlDBType == "SQL")
            {
                dt = DBsql.RunDataTable(sql1, out errMsg);
            }
            else
            {
                dt = DBdb2.RunDataTable(sql1, out errMsg);
            }
            if (dt == null || dt.Rows.Count == 0)
            {
                string sql2 = "insert into 班次时间表 (班次编号,班次描述,起始时间,结束时间) values ('" + BcId + "','" + BcMs + "','" + ST + "','" + ET + "')";
                if (rlDBType == "SQL")
                {
                    DBsql.RunNonQuery(sql2, out errMsg);
                }
                else
                {
                    DBdb2.RunNonQuery(sql2, out errMsg);
                }
                if (errMsg == "")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        public bool UpadteBC(string OBcId, string BcId, string BcMs, string ST, string ET, out string errMsg)
        {
            errMsg = "";
            this.init();
            if (OBcId == BcId)
            {
                string sql = "update 班次时间表 set 班次描述='" + BcMs + "',起始时间='" + ST + "',结束时间='" + ET + "' where 班次编号='" + OBcId + "'";
                if (rlDBType == "SQL")
                {
                    DBsql.RunNonQuery(sql, out errMsg);
                }
                else
                {
                    DBdb2.RunNonQuery(sql, out errMsg);
                }
                if (errMsg == "")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                string sql1 = "select * from 班次时间表 where 班次编号='" + BcId + "'";

                DataTable dt;

                if (rlDBType == "SQL")
                {
                    dt = DBsql.RunDataTable(sql1, out errMsg);
                }
                else
                {
                    dt = DBdb2.RunDataTable(sql1, out errMsg);
                }
                if (dt == null || dt.Rows.Count == 0)
                {
                    string sql2 = "update 班次时间表 set 班次编号='" + BcId + "',班次描述='" + BcMs + "',起始时间='" + ST + "',结束时间='" + ET + "' where 班次编号='" + OBcId + "'";
                    if (rlDBType == "SQL")
                    {
                        DBsql.RunNonQuery(sql2, out errMsg);
                    }
                    else
                    {
                        DBdb2.RunNonQuery(sql2, out errMsg);
                    }
                    if (errMsg == "")
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
        }

        public bool DeleteBC(string BcId, out string errMsg)
        {
            errMsg = "";
            bool flag = true;
            this.init();

            string sql = "delete from 班次时间表 where 班次编号='" + BcId + "'";
            if (rlDBType == "SQL")
            {
                DBsql.RunNonQuery(sql, out errMsg);
            }
            else
            {
                DBdb2.RunNonQuery(sql, out errMsg);
            }
            if (errMsg == "")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public ArrayList BClist(out string errMsg)
        {
            ArrayList BClist = new ArrayList();
            string[] bc1 = new string[45];
            string[] bc2 = new string[45];
            string[] bc3 = new string[45];
            string[] bc4 = new string[45];
            this.init();
            errMsg = "";
            string sql = "select 班次编号,班次描述,起始时间,结束时间 from 班次时间表 order by 班次编号";
            DataTable dt;
            if (rlDBType == "SQL")
            {
                dt = DBsql.RunDataTable(sql, out errMsg);
            }
            else
            {
                dt = DBdb2.RunDataTable(sql, out errMsg);
            }
            if (dt.Rows.Count > 0 && dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    bc1[i] = dt.Rows[i]["班次编号"].ToString();
                    bc2[i] = dt.Rows[i]["班次描述"].ToString();
                    bc3[i] = dt.Rows[i]["起始时间"].ToString();
                    bc4[i] = dt.Rows[i]["结束时间"].ToString();
                }
            }
            else
            {
                bc1 = null;
                bc2 = null;
                bc3 = null;
                bc4 = null;
            }
            BClist.Add(bc1);
            BClist.Add(bc2);
            BClist.Add(bc3);
            BClist.Add(bc4);

            return BClist;
        }

        public ArrayList BZlist(out string errMsg)
        {
            ArrayList BZlist = new ArrayList();
            string[] bz1 = new string[45];
            string[] bz2 = new string[45];
            this.init();
            errMsg = "";
            string sql = "select 班组编号,班组描述 from 班组表 order by 班组编号";
            DataTable dt;
            if (rlDBType == "SQL")
            {
                dt = DBsql.RunDataTable(sql, out errMsg);
            }
            else
            {
                dt = DBdb2.RunDataTable(sql, out errMsg);
            }
            if (dt.Rows.Count > 0 && dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    bz1[i] = dt.Rows[i]["班组编号"].ToString();
                    bz2[i] = dt.Rows[i]["班组描述"].ToString();
                }
            }
            else
            {
                bz1 = null;
                bz2 = null;

            }
            BZlist.Add(bz1);
            BZlist.Add(bz2);

            return BZlist;
        }

        public bool EmptyPB(out string errMsg)
        {
            errMsg = "";
            bool flag = true;
            this.init();
            string sql = "delete from 排班表";
            if (rlDBType == "SQL")
            {
                DBsql.RunNonQuery(sql, out errMsg);
            }
            else
            {
                DBdb2.RunNonQuery(sql, out errMsg);
            }
            if (errMsg == "")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public string BZIDbyBZMS(string bzms, out string errMsg)
        {
            errMsg = "";
            this.init();
            string[] bzid = new string[45];
            string sql = "select 班组编号 from 班组表 where 班组描述='" + bzms + "'";
            DataTable dt;
            if (rlDBType == "SQL")
            {
                dt = DBsql.RunDataTable(sql, out errMsg);
            }
            else
            {
                dt = DBdb2.RunDataTable(sql, out errMsg);
            }
            if (dt.Rows.Count > 0 && dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    bzid[i] = dt.Rows[i]["班组编号"].ToString();
                }
            }
            return bzid[0];
        }

        public bool InsertPB(string sqldb2, string sqlsql, out string errMsg)
        {
            errMsg = "";
            bool flag = false;
            this.init();

            if (rlDBType == "SQL")
            {
                DBsql.RunNonQuery(sqlsql, out errMsg);
            }
            else
            {
                DBdb2.RunNonQuery(sqldb2, out errMsg);
            }
            if (errMsg == "")
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        private string init()
        {
            rlDBType = IniHelper.ReadIniData("RelationDBbase", "DBType", null);
            rtDBType = IniHelper.ReadIniData("RTDB", "DBType", null);
            pGl1 = IniHelper.ReadIniData("Report", "FH1", null);
            pGl2 = IniHelper.ReadIniData("Report", "FH2", null);

            return rlDBType;
        }

        public string GetDBtype()
        {
            this.init();
            string DBtype = rlDBType;
            return DBtype;
        }

    }
}
